package util

import (
	"strconv"
	"strings"

	"mall/common/globalkey"
	"mall/common/utils/pointy"
	"mall/service/order/rpc/orderclient"
)

const (
	arrayValueSeparator    = ","
	keyValueSeparator      = ":"
	keyOpSeparator         = ","
	arrayKeyValueSeparator = ";"
)

func ParseSorter(sorter *string) (res []*orderclient.OrderBy) {
	if sorter == nil {
		return
	}

	sorters := strings.Split(*sorter, arrayKeyValueSeparator)
	if len(sorters) == 0 {
		return
	}

	for _, orderBy := range sorters {
		orderBys := strings.Split(orderBy, keyValueSeparator)

		if len(orderBys) == 2 && len(orderBys[0]) > 0 && len(orderBys[1]) > 0 {
			res = append(res, &orderclient.OrderBy{
				Field: orderBys[0],
				Order: orderBys[1],
			})
			continue
		}

		if len(orderBys) == 1 && len(orderBys[0]) > 0 {
			res = append(res, &orderclient.OrderBy{
				Field: orderBys[0],
				Order: "0",
			})
		}
	}

	return
}

func ParseFilter(filter *string, orderType *uint32) (res []*orderclient.FilterBy) { // 2024.01.27
	if filter != nil {
		filters := strings.Split(*filter, arrayKeyValueSeparator)
		if len(filters) > 0 {
			for _, filterBy := range filters {
				filterBys := strings.Split(filterBy, keyValueSeparator)

				if len(filterBys) == 2 && len(filterBys[0]) > 0 && len(filterBys[1]) > 0 {
					res = append(res, &orderclient.FilterBy{
						Field: filterBys[0],
						In:    filterBys[1],
					})
				}
			}
		}
	}

	if orderType != nil && *orderType > 0 {
		res = append(res, &orderclient.FilterBy{
			Field: "type",
			In:    strconv.FormatInt(int64(*orderType), 10),
		})
	}

	return
}

func GetProductTypeText(productType *uint32) (res *string) { // 2024.08.13
	if productType != nil {
		switch *productType {
		case globalkey.ProductTypePhysical:
			res = pointy.GetPointer("实物商品")
		case globalkey.ProductTypeVirtual:
			res = pointy.GetPointer("虚拟物品")
		case globalkey.ProductTypeCommon:
			res = pointy.GetPointer("原装正品")
		case globalkey.ProductTypeSample:
			res = pointy.GetPointer("试香小样")
		case globalkey.ProductTypeUnused:
			res = pointy.GetPointer("闲置")
		}
	}

	return
}

func GetOrderTypeText(orderType *uint32) (res *string) { // 2024.01.26
	if orderType != nil {
		switch *orderType {
		case globalkey.OrderTypeVirtual:
			res = pointy.GetPointer("宝石订单")
		case globalkey.OrderTypeCommon:
			res = pointy.GetPointer("实物订单")
		}
	}

	return
}

func GetTradeStateText(tradeState *uint32) (res *string) { // 2024.01.26
	if tradeState != nil {
		switch *tradeState {
		case globalkey.OrderTradeStateUserCancelled:
			res = pointy.GetPointer("取消支付")
		case globalkey.OrderTradeStateAdminCancelled:
			res = pointy.GetPointer("超时取消支付")
		case globalkey.OrderTradeStateAutoCancelled:
			res = pointy.GetPointer("超时取消支付")
		case globalkey.OrderTradeStateNotPay:
			res = pointy.GetPointer("待支付")
		case globalkey.OrderTradeStateAdminRefunded:
			res = pointy.GetPointer("已退款")
		case globalkey.OrderTradeStateAdminRefunding:
			res = pointy.GetPointer("退款中")
		case globalkey.OrderTradeStateRefunded:
			res = pointy.GetPointer("已退款")
		case globalkey.OrderTradeStateRefunding:
			res = pointy.GetPointer("退款中")
		case globalkey.OrderTradeStateRefundApply:
			res = pointy.GetPointer("已申请退款")
		case globalkey.OrderTradeStatePaid:
			res = pointy.GetPointer("已支付，待发货")
		case globalkey.OrderTradeStateReturned:
			res = pointy.GetPointer("已退货")
		case globalkey.OrderTradeStateReturning:
			res = pointy.GetPointer("退货中")
		case globalkey.OrderTradeStateReturnApply:
			res = pointy.GetPointer("已申请退货")
		case globalkey.OrderTradeStateShipping:
			res = pointy.GetPointer("备货中")
		case globalkey.OrderTradeStateShipped:
			res = pointy.GetPointer("已发货")
		case globalkey.OrderTradeStateDelivering:
			res = pointy.GetPointer("配送中")
		case globalkey.OrderTradeStateDelivered:
			res = pointy.GetPointer("已配送")
		case globalkey.OrderTradeStateConfirmed:
			res = pointy.GetPointer("确认收货")
		case globalkey.OrderTradeStateAutoConfirmed:
			res = pointy.GetPointer("自动确认收货")
		case globalkey.OrderTradeStateReviewed:
			res = pointy.GetPointer("已评价")
		case globalkey.OrderTradeStateAutoReviewed:
			res = pointy.GetPointer("自动评价")
		case globalkey.OrderTradeStateFulfilled:
			res = pointy.GetPointer("已完成")
		case globalkey.OrderTradeStateClosed:
			res = pointy.GetPointer("已关闭")
		}
	}

	return
}

type Option struct {
	CanAdjust      bool
	CanRemove      bool
	CanRestore     bool
	CanForceDelete bool
	CanSync        bool
}

func GetOptionByTradeState(tradeState *uint32) *Option { // 2024.01.26
	res := &Option{}

	if tradeState != nil {
		switch uint32(*tradeState) {
		case globalkey.OrderTradeStateNotPay:
			res.CanAdjust = true
		case globalkey.OrderTradeStateAdminDeleted:
			res.CanRestore = true
			res.CanForceDelete = true
		case globalkey.OrderTradeStatePaid:
			res.CanSync = true
		}
	}

	return res
}
